#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1e5 + 10;

int main()
{
	int n, cnt = 0;
	cin >> n;
	string name, birth, maxname, minname, maxbirth = "1814/09/06", minbirth = "2014/09/06";
	
	for(int i = 0; i < n; i ++)
	{
		cin >> name >> birth;
		if(birth >= "1814/09/06" && birth <= "2014/09/06")
		{
			cnt ++;
			if(birth >= maxbirth)
			{
				maxname = name;
				maxbirth = birth;
			}
			if(birth <= minbirth)
			{
				minname = name;
				minbirth = birth;
			}
		}
	}

	cout << cnt;
	if(cnt) cout << " " << minname << " " << maxname;
	return 0;
}